package com.fingard.dsp.bank.directbank.wjrb01;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.net.WebRequest;
import com.fingard.text.StringHelper;
import com.fingard.util.TransIDHelper;
import com.fingard.xml.XmlTextWriter;

import java.util.Date;
/**
 * @author: liuxb
 * @create: 2019-12-17 14:19
 * @version: 1.0
 **/
public class WJRB01Base extends DirectBase {
    protected static TransIDHelper idHelper = new TransIDHelper(1, 9999);

    /**
     * 获取交互字符集
     *
     * @return
     **/
    protected String getCharset() {
        return getBankConfig().getCharset("UTF-8");
    }

    /**
     * 写通用公共报文头
     */
    protected void writeCommonHeader(XmlTextWriter xmlWriter, WJRB01Header p_bankHeader) throws Exception {
        if (StringHelper.isNullOrEmpty(p_bankHeader.TrsCode)) {
            throw new Exception("未赋值serviceId");
        }
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("Message");
        xmlWriter.writeStartElement("Head");
        xmlWriter.writeElementString("TrsCode", p_bankHeader.TrsCode);//银行交易码，用以区分不同的操作
        Date tmpDateNow = new Date();
        xmlWriter.writeElementString("ERPTrsTimestamp", Format.DateTimeFormat.format(tmpDateNow));//交易时间(格式为：yyyyMMddHHmmss)
        xmlWriter.writeElementString("ERPJnlNo ", p_bankHeader.ERPJnlNo);//ERP（财务系统）交易流水号
        xmlWriter.writeEndElement();
    }

    protected void writeCommonHeader2(XmlTextWriter xmlWriter, WJRB01Header p_bankHeader) throws Exception {
        if (StringHelper.isNullOrEmpty(p_bankHeader.TrsCode)) {
            throw new Exception("未赋值serviceId");
        }
        xmlWriter.writeStartDocByDeclaration("<?xml version=\"1.0\" encoding=\"" + getCharset() + "\"?>");
        xmlWriter.writeStartElement("Message");
        xmlWriter.writeStartElement("Head");
        xmlWriter.writeElementString("TrsCode", p_bankHeader.TrsCode);//银行交易码，用以区分不同的操作
        Date tmpDateNow = new Date();
        xmlWriter.writeElementString("ERPTrsTimestamp", Format.DateTimeFormat.format(tmpDateNow));//交易时间(格式为：yyyyMMddHHmmss)
        xmlWriter.writeEndElement();
    }


    protected String[] sendToBank(String p_reqStr, WJRB01Header header) throws Exception {
        String[] result = {"", ""};
        String tmpUrl = bankFront.serverURL + header.TrsCode + ".do?userPassword=" + bankFront.userPassword + "&SIGDATA=1";
        WriteBankLogLn("1.提交银行的地址：" + tmpUrl);
        WriteBankLogStep2(p_reqStr);
        WebRequest tmpWebRequest = new WebRequest(tmpUrl);
        tmpWebRequest.setConnectTimeout(100000);//设置连接超时
        tmpWebRequest.setRequestProperty("Method", "POST");//设置请求属性
        tmpWebRequest.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");//设置请求属性
        tmpWebRequest.setRequestProperty("connection", "close");//设置连接状态
        byte[] tmpUpBytes = p_reqStr.getBytes(getCharset());
        result = tmpWebRequest.upload(tmpUpBytes, getCharset());
        WriteBankLogStep3(result[1]);
        return result;
    }

    /**
     * 生成唯一交易流水号
     */
    protected String getQryTransID() {
        return Format.DateTime12Year2Format.format(new Date()) + idHelper.getNextIDStr();
    }


}
